//
//  ManageTieZiTableViewCell.m
//  Lucky28
//
//  Created by zr on 2018/8/2.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ManageTieZiTableViewCell.h"

@interface ManageTieZiTableViewCell ()

@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, strong)UIButton * sayButton;
@property (nonatomic, strong)UIButton * collectionButton;
@property (nonatomic, strong)UIButton * likeButton;
@property (nonatomic, strong)UIButton * deleteButton;
@property (nonatomic, weak)UIImageView * contentImageView;
@property (nonatomic, weak)UIView * lineView;

@property (nonatomic, copy)NSString * ID;

@end

@implementation ManageTieZiTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setModel:(ManageTieZiModel *)model{
    
    _model = model;
    
    NSURL * url = [NSURL URLWithString:model.headimg];
    
    [self.iconImageView sd_setImageWithURL:url placeholderImage:ImageNamed(@"头像7")];
    self.nameLabel.text = model.username;
    
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    //    NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
    
    self.timeLabel.text = dateString;
    
    self.contentLabel.text = model.title;
    
    int is_collect = [model.is_collect intValue];
    if (is_collect == 1) {
        
        self.collectionButton.selected = YES;
    }else if (is_collect == 0){
        
        self.collectionButton.selected = NO;
    }
    
    int is_praise = [model.is_praise intValue];
    if (is_praise == 1) {
        
        self.likeButton.selected = YES;
    }else if (is_praise == 0){
        
        self.likeButton.selected = NO;
    }
    
    int comment_count = [model.comment_count intValue];
    int praise = [model.praise intValue];
    
    [self.likeButton setTitle:[NSString stringWithFormat:@"  %d",comment_count] forState:UIControlStateNormal];
    [self.likeButton setTitle:[NSString stringWithFormat:@"  %d",praise] forState:UIControlStateNormal];
    
    NSString * imageStr = model.images.firstObject;
    NSURL * url1 = [NSURL URLWithString:imageStr];
    
    [self.contentImageView sd_setImageWithURL:url1];
    
    self.ID = model.ID;
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView * iconImageView = [UIImageView new];
    
    //  把图片设置成圆形
    iconImageView.layer.cornerRadius = 25;//裁成圆角
    iconImageView.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    
//    iconImageView.image = ImageNamed(@"头像7");
    
    self.iconImageView = iconImageView;
    
    [self.contentView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.text = @"用户姓名";
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.nameLabel = nameLabel;
    
    [self.contentView addSubview:self.nameLabel];
    
    
    self.timeLabel = [self setupLabelWith:@"15分钟前"];
    [self.contentView addSubview:self.timeLabel];

    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.text = @"跟着大神走，果真中了！";
    contentLabel.textColor = [UIColor blackColor];
    contentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.contentLabel = contentLabel;
    
    [self.contentView addSubview:self.contentLabel];
    
    
    UIButton * collectionButton = [UIButton new];
    
    [collectionButton setImage:ImageNamed(@"shoucan") forState:UIControlStateNormal];
    [collectionButton setImage:ImageNamed(@"shoucan2") forState:UIControlStateSelected];
    [collectionButton addTarget:self action:@selector(clickCollectionButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.collectionButton = collectionButton;
    
    [self.contentView addSubview:self.collectionButton];
    
//    120 * 150
    UIImageView * contentImageView = [UIImageView new];
    
    contentImageView.backgroundColor = [UIColor whiteColor];
    
    self.contentImageView = contentImageView;
    
    [self.contentView addSubview:self.contentImageView];
    
    
    self.likeButton = [self setupButtonWith:@"0" withImage:@"dianzan"];
    self.likeButton.selected = NO;
    [self.likeButton setImage:ImageNamed(@"dianzan2") forState:UIControlStateSelected];
    [self.contentView addSubview:self.likeButton];
    
    self.sayButton = [self setupButtonWith:@"0" withImage:@"pinglun"];
    [self.contentView addSubview:self.sayButton];
    
    self.deleteButton = [self setupButtonWith:@"删除" withImage:@"shanchu"];
    [self.contentView addSubview:self.deleteButton];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xB5B5B5);
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
}


- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(50 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.top.mas_equalTo(self).offset(20 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(15 * SCREENH6);
        make.top.mas_equalTo(self).offset(25 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.collectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(50 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(35 * SCREENH6);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(120 * SCREENH6);
        make.height.mas_equalTo(150 * SCREENH6);
        make.leading.mas_equalTo(self.contentLabel);
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentImageView.mas_trailing);
        make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.likeButton);
    }];
    
    [self.sayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-88 * SCREENH6);
        make.centerY.mas_equalTo(self.deleteButton);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.leading.trailing.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
}

- (UIButton *)setupButtonWith:(NSString *)string withImage:(NSString *)iamgeString{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:[NSString stringWithFormat:@"  %@",string] forState:UIControlStateNormal];
    [button setImage:ImageNamed(iamgeString) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:ColorWithRGB(0xB5B5B5) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    
    return button;
}


- (UILabel *)setupLabelWith:(NSString *)string{
    
    UILabel * label = [UILabel new];
    
    label.text = string;
    label.textColor = ColorWithRGB(0xB5B5B5);
    label.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    return label;
}


- (void)clickBottomButton:(UIButton *)button{
    
//    NSString * type;
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];    
    NSString * user_id = [userDefaults objectForKey:@"user_id"];
    NSString * token = [userDefaults objectForKey:@"token"];
    
    if (button == self.likeButton) {
        
        NSDictionary * dict = @{@"user_id":user_id,@"note_id":self.ID,@"type":@"1",@"token":token};
        
        [[NetworkTool sharedTools] POST:@"/index/Note/addCollect" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
//            NSLog(@"res===%@",responseObject);
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ReLoadDataForTableview" object:nil];
            }else{
                
                NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(didCellNSString:)]) {
                    [self.delegate didCellNSString:string];
                }

            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }else if (button == self.sayButton){
        
        
    }else if (button == self.deleteButton){
        
        NSDictionary * dict = @{@"note_id":self.ID,@"token":token};
        
        [[NetworkTool sharedTools] POST:@"/index/Note/del" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"res===%@",responseObject);
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ReLoadDataForTableview" object:nil];
            }else{
                
                NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(didCellNSString:)]) {
                    [self.delegate didCellNSString:string];
                }
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    //回调
}

- (void)clickCollectionButton{
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSString * user_id = [userDefaults objectForKey:@"user_id"];
    NSString * token = [userDefaults objectForKey:@"token"];
    
    if (!self.collectionButton.selected) {
        
        self.collectionButton.selected = YES;
        
        NSDictionary * dict = @{@"user_id":user_id,@"note_id":self.ID,@"type":@"2",@"token":token};
        
        [[NetworkTool sharedTools] POST:@"/index/Note/addCollect" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
//                        NSLog(@"res===%@",responseObject);
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ReLoadDataForTableview" object:nil];
            }else{
                
                NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(didCellNSString:)]) {
                    [self.delegate didCellNSString:string];
                }
                
            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }else{
        
        self.collectionButton.selected = NO;
        
        NSDictionary * dict = @{@"user_id":user_id,@"note_id":self.ID,@"type":@"2",@"token":token,@"is_cancel":@"1"};
        
        [[NetworkTool sharedTools] POST:@"/index/Note/addCollect" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
//            NSLog(@"res===%@",responseObject);
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ReLoadDataForTableview" object:nil];
            }else{
                
                NSString * string = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(didCellNSString:)]) {
                    [self.delegate didCellNSString:string];
                }
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
